#include "ImageManager.h"

Image& ImageManager::getImage(string filename) {
    // Check if image is already loaded
    for (map<string, Image>::iterator it = images.begin(); it != images.end(); it++) {
        if (filename == it->first) {
            return it->second;
        }
    }
    
    // Image is not yet loaded; add it to memory
    Image img;
    img.LoadFromFile(filename);
    img.CreateMaskFromColor(COLOR_MASK);
    images[filename] = img;
    
    return images[filename];
}

Image& ImageManager::getImage(string filename, string path) {
    return getImage(path + filename);
}

void ImageManager::deleteImage(string filename) {
    for (map<string, Image>::iterator it = images.begin(); it != images.end(); it++) {
        if (filename == it->first) {
            images.erase(it);
        }
    }
}

void ImageManager::deleteImage(Image& img) {
    for (map<string, Image>::iterator it = images.begin(); it != images.end(); it++) {
        if (&img == &it->second) {
            images.erase(it);
        }
    }
}